27. Find the Longest Name
Find the longest name
Question:
Start Quiz:
public String findLongestName(String [] names){
return "";
}
Solution:
Quiz hints
You should implement the function String findLongestName(String [] names) which takes an array of Strings as an input containing a list of names, and return the String that has the longest name.
To do so, try to follow these steps:
The first step is to calculate and store the length of the input array, this is done using
names.length;and store that in an integer variable.Then create a new String called
longestNamethat will store the longest name in the array of names, initialize it with the first name in the array, that isnames[0].Next, you should create a
forloop that will compare every name in the array usingnames[i]against thelongestName. Only replace thelongestNamevalue if thenames[i]is longer .Finally, return the
longestNamevariable as the return value of the function.